#!/usr/bin/env python

import unittest

from google.appengine.ext import db
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_util

import mp

class TestModel(db.Model):
    def __eq__(self, other):
        return self.key() == other.key()
    
    def __repr__(self):
        return unicode(self.key())

class TestModelPlus(mp.Model):
    def __eq__(self, other):
        return self.key() == other.key()

    def __repr__(self):
        return unicode(self.key())

class TestModelPlusPrefetch(mp.Model):
    ref1 = db.ReferenceProperty(TestModelPlus)
    ref2 = db.ReferenceProperty(TestModel)
    
    def __eq__(self, other):
        return self.key() == other.key()

    def __repr__(self):
        return unicode(self.key())
    
class MethodsTestCase(unittest.TestCase):
    def setUp(self):
        mp.DEBUG = True
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        
    def tearDown(self):
        mp.DEBUG = False
        self.testbed.deactivate()
        
    def testSimple(self):
        key = mp.put(TestModel())
        refs = TestModel.all().fetch(2)
        self.assertEqual(1, len(refs))
        self.assertEqual(refs[0], mp.get(key))
        mp.delete(key)
        self.assertEqual(0, TestModel.all().count(0))
        self.assertEqual(None, mp.get(key))

    def testSimpleWithKeyName(self):
        key = mp.put(TestModel(key_name="test1"))
        refs = TestModel.all().fetch(2)
        self.assertEqual(1, len(refs))
        self.assertEqual(refs[0], mp.get(key))
        mp.delete(key)
        self.assertEqual(0, TestModel.all().count(0))
        self.assertEqual(None, mp.get(key))

    def testMultiple(self):
        keys = mp.put([TestModel(), TestModel()])
        refs = TestModel.all().fetch(3)
        self.assertEqual(2, len(refs))
        self.assertEqual(len(refs), len(mp.get(keys)))
        mp.delete(keys)
        self.assertEqual(0, TestModel.all().count(0))

    def testMultipleWithKeyName(self):
        keys = mp.put([TestModel(key_name='test1'), TestModel()])
        refs = TestModel.all().fetch(3)
        self.assertEqual(2, len(refs))
        self.assertEqual(len(refs), len(mp.get(keys)))
        mp.delete(keys)
        self.assertEqual(0, TestModel.all().count(0))


    def testModelPlus(self):
        m = TestModelPlus()
        key = m.put()
        self.assertEqual(m, mp.get(key))
        self.assertEqual(m, TestModelPlus.get(key))
        self.assertEqual(m, TestModelPlus.get_by_id(m.key().id()))
        m.delete()
        self.assertEqual(0, TestModel.all().count(0))

        m = TestModelPlus(key_name='test3')
        key = m.put()
        self.assertEqual(m, mp.get(key))
        self.assertEqual(m, TestModelPlus.get_by_key_name(m.key().name()))
        m.delete()
        self.assertEqual(0, TestModel.all().count(0))

    def testModelPrefetch(self):
        ref1=TestModelPlus(key_name='test4')
        ref2=TestModel(key_name='test5')
        m = TestModelPlusPrefetch(ref1=ref1,  ref2=ref2)
        k = m.put()
        mp.put([ref1, ref2])

        p = mp.prefetch(mp.get(k))
        p.prefetch()
        
        
